<?php
class MongoModel extends Model {

	//MongoDB连接
	public function _connect($collection) {
		//$collection 集合名称
		$mogodb = array('host' => C('DB_MONGO.db_host'), 'port' => C('DB_MONGO.db_port'), 'user' => C('DB_MONGO.db_user'), 'passwd' => C('DB_MONGO.db_pwd'), 'dbname' => C('DB_MONGO.db_name'));

		$m = new MongoClient("mongodb://{$mogodb['host']}:{$mogodb['port']}", array('username' => $mogodb['user'], 'password' => $mogodb['passwd'], 'db' => $mogodb['dbname']));

		$db = $m -> peisong;
		//选择一个数据库(peisong)
		$collections = $db -> $collection;
		return $collections;
	}

	//插入数据
	public function add($data,$collection='dayorder') {
		$add=$this->_connect($collection) -> insert($data);
		return $add;
	}
	
	//删除文档
	public function remove($where,$collection='dayorder'){
		if($where=='' or $collection==''){
			return FALSE;
		}
		$re=$this->_connect($collection)->remove(array('wid'=>$where['wid']));
		return $re;
	}
	
	//单条条件查找
	public function _find($where,$collection='dayorder'){
		$finded=$this->_connect($collection)->find($where)->skip(0);
		foreach($finded as $vl){
				//转换ID
				foreach($vl['_id'] as $ise){
					$vl['_id']=$ise;
				}
				$es[]=$vl;
		}
		
		return $es;
	}
	
	//查找数据
	public function find($where,$sort,$limit=15,$skip=0,$collection='dayorder'){
		/*
		 * 集合名$collection=>'collection_name';
		 * 条件 $where=>array("name"=>value);
		 * 条数 $limit=>15;
		 * 排序方式(1顺序，-1倒序) $sort=>array("name"=>1,"name2"=>-1);
		 * 开始条数 $skip=0;
		 * 计算条数 $count=true;
		 * */
		if(!isset($sort)){
			$find=$this->_connect($collection)->find($where)->limit($limit)->skip($skip);
		}else{
			if($where==''){
				$find=$this->_connect($collection)->find()->limit($limit)->skip($skip);
			}else{
				$find=$this->_connect($collection)->find($where)->sort($sort)->limit($limit)->skip($skip);
			}
		}
		
		foreach($find as $vl){
				//转换ID
				foreach($vl['_id'] as $ise){
					$vl['_id']=$ise;
				}
				$es[]=$vl;
		}
		
		return $es;
	}
	
	public function findall($where,$collection='dayorder'){
		if($where==''){
			$finded=$this->_connect($collection)->find();
		}else{
			$finded=$this->_connect($collection)->find($where)->skip(0);
		}
		foreach($finded as $vl){
				//转换ID
				foreach($vl['_id'] as $ise){
					$vl['_id']=$ise;
				}
				$finds[]=$vl;
		}
		return $finds;
	}

	//分页查找数据
	public function Pagefind($where,$p=1,$status,$sort,$limit=15,$collection='dayorder'){
		/*
		 * 文件名$collection='document name'
		 * 条件 $where=array("name"=>value);
		 * 条数 $limit=15
		 * 开始页面数 $status=0;
		 * */
		
		import('@.ORG.Page');
		
		if($where['teamid']){
			$wheres['teamid']=$where['teamid'];
		}
		
		$count = count($this->findall($wheres,$collection));
		/*echo date("Y-m-d",$where['docid']['$gt']);*/
		
		$skp=($p-1)*$limit;

		$userlist = $this->find($wheres,$sort,$limit,$skp,$collection);
		
		if($collection=='puser'){
			//配送员合并数据
			foreach($userlist as $key=>$vo){
				$mer=$this->merge($vo["psid"],$where);
				if($mer){
					$conlist[$key]=$mer;
				}
			}
		}elseif($collection=='teamname'){
			//团队合并数据
			foreach($userlist as $key=>$vo){
				$mer=$this->Tmerge($vo["teamid"],$where);
				if($mer){
					$conlist[$key]=$mer;
				}
			}
		}else{
			$conlist=$userlist;
		}

		$Page = new Page($count,$limit);
		if($status){
			if(!$status['teamid']){
				$Page->parameter = 't_start='.$status['t_start']."&t_end=".$status['t_end'];
			}else{
				$Page->parameter = 't_start='.$status['t_start']."&t_end=".$status['t_end']."&teamid=".urlencode($status['teamid']);
			}
		}
		
//		dump($where);exit;
		$show  = $Page->show();
		return array("page"=>$show,"userlist"=>$conlist);
	}
	
	
	//配送员数据合并
	function merge($uid,$where){
		
		$where['psid']=$uid;
		
		$finds=$this->_find($where);
		
		//开始合并
		foreach($finds as $vel){
			$fdata["docid"] = $vel['docid'];
		    $fdata["adddate"] = $vel["adddate"];
		    $fdata["psname"] =$vel["psname"];
		    $fdata["psid"] = $vel["psid"];
		    $fdata["city"] = $vel["city"];
		    $fdata["teamname"] = $vel["teamname"];
		    $fdata["teamid"] = $vel["teamid"];
		    $fdata["ordered"] += $vel["ordered"];
		    $fdata["valid"] += $vel["valid"];
		    $fdata["40min"] += $vel["40min"];
		    $fdata["40_60min"] += $vel["40_60min"];
		    $fdata["timeout"] += $vel["timeout"];
		    $fdata["40points"] += $vel["40points"];
		    $fdata["unusual"] += $vel["unusual"];
		    $fdata["bad_review"] += $vel["bad_review"];
		    $fdata["mid_review"] += $vel["mid_review"];
		    $fdata["good_review"] += $vel["good_review"];
		    $fdata["com_volume"] += $vel["com_volume"];
		    $fdata["com_sended"] += $vel["com_sended"];
		    $fdata["com_badservice"] += $vel["com_badservice"];
		    $fdata["com_business"] += $vel["com_business"];
		}
		
		return $fdata;
	}

	//团队数据合并
	function Tmerge($tid,$where){
		$where['teamid']=$tid;
		
		$finds=$this->_find($where,'team');
//		dump($where);exit;
		//开始合并
		foreach($finds as $k=>$vel){
			$fdata["docid"] = $vel['docid'];
		    $fdata["adddate"] = $vel["adddate"];
		    $fdata["city"]=$vel["city"];
			$fdata["teamid"]=$vel["teamid"];
			$fdata["teamname"]=$vel["teamname"];
			$fdata["tuidan"]+=$vel["tuidan"];
			$fdata["zhudong"]+=$vel["zhudong"];
			$fdata["teamjie"]+=$vel["teamjie"];
			$fdata["peisonged"]+=$vel["peisonged"];
			$fdata["qizha"]+=$vel["qizha"];
			$fdata["ordered"]+=$vel["ordered"];
			$fdata["js_order"]+=$vel["js_order"];
			$fdata["bad_com"]+=$vel["bad_com"];
			$fdata["mid_com"]+=$vel["mid_com"];
			$fdata["good_com"]+=$vel["good_com"];
			$fdata["40min"]+=$vel["40min"];
			$fdata["yc_order"]+=$vel["yc_order"];
			$fdata["caoshi"]+=$vel["caoshi"];
			$fdata["dd_caoshi"]+=$vel["dd_caoshi"];
			$fdata["toushu"]+=$vel["toushu"];
			$fdata["p_yc"]+=$vel["p_yc"];
			$fdata["p_40min"]+=$vel["p_40min"];
			$fdata["p_cs"]+=$vel["p_cs"];
			$fdata["p_ts"]+=$vel["p_ts"];
		}
		return $fdata;
	}
	
	//更新
	function update($where,$data,$collection){
		$update=$this->_connect($collection)->update($where,$data);
		return $update;
	}
}
?>